package com.tp2.etna.ejemplo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tp2.etna.ejemplo.modelo.Empleado;
import com.tp2.etna.ejemplo.service.EmpleadoService;

@Controller
public class EjemploController {

	@Autowired
	private EmpleadoService empleadoService;
	
	@RequestMapping(value="/welcome", method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

		model.addAttribute("message", "Spring 3 MVC Hello World");
		return "index";

	}
	
	@RequestMapping(value="/ejemplo", method = RequestMethod.GET)
	public String ejemplo(ModelMap model) {

		return "ejemplo";

	}
	
	@RequestMapping(value="/empleado/{id}", method = RequestMethod.GET,produces={"application/json"})
	public @ResponseBody Empleado obtenerEmpleado(ModelMap model,@PathVariable("id") int id) {
		return empleadoService.obtenerEmpleado(id);
	}
	
	
	@RequestMapping(value="/empleadoUpd/{id}", method = RequestMethod.POST,produces={"application/json"})
	public String  actualizar2(@RequestBody Empleado empleado,@PathVariable("id") int id) {
		empleado.setId(id);
		empleadoService.actualizarEmpleado(empleado);
		return "ejemplo";
	}
	
}